<?php
//生产工序

namespace app\admin\controller;

use app\admin\model\Component;
use app\admin\model\CompReCompPurch;
use app\admin\model\Projects;
use app\admin\model\ProjectsOrder;
use app\admin\service\User as UserS;
class ProduceProcesses extends Base
{

    protected $middleware = ['app\admin\middleware\TableFields::class'];  //获取表结构，添加在返回数据中
    protected $lists_fields = 'id,component_type,drawing_no,drawing_name,projects_id,projects_order_id,create_time,processes_id,production_status,responsible_id,notes,status'; //查询字段

    public $additional_fields_list = [
        'project_title' =>['name' => 'project_title', 'comment' => '项目'],
        'order_name' => ['name' => 'order_name', 'comment' => '订单'],
        'p_name' => ['name' => 'p_name', 'comment' => '父级'],
        'son_num' => ['name' => 'son_num', 'comment' => '子级'],
        'number' => ['name' => 'number', 'comment' => '数量'],
        'purchased_lists' => ['name' => 'purchased_lists', 'comment' => '外购件信息'],
        'estimated_delivery_time' => ['name' => 'estimated_delivery_time', 'comment' => '交付时间'],
        'responsible' => ['name' => 'responsible', 'comment' => '负责人'],
        'qrcode' => ['name' => 'qrcode', 'comment' => '二维码'],
    ];


    public function filter(&$map, &$field)
    {
        $field = $this->lists_fields;

        //工序查询
        $processes_id = $this->request->get('processes_id');
        if(!empty($processes_id))
        {
            $map[] = ['processes_id', '=', $processes_id];
        }

        //生产状态_1：待制作，2：制作中，3：已完成，4：异常
        $production_status = input('production_status');
        if(!empty($production_status))
        {
            $map[] = ['production_status', '=', $production_status];
        }

        //图名
        $drawing_no = input('drawing_no');
        if(!empty($drawing_no)){
            $map[] = ['drawing_no', 'like', '%'.$drawing_no.'%'];
        }

        //图号
        $drawing_name = input('drawing_name');
        if(!empty($drawing_name)){
            $map[] = ['drawing_name', 'like', '%'.$drawing_name.'%'];
        }

        //创建时间
        $start_time = input('start_time', 0);
        $end_time = input('end_time', 0);
        if (!empty($end_time) && !empty($start_time)) {
            $map[] = ['create_time', 'BETWEEN', [$start_time, $end_time]];
        } else if ($end_time) {
            $map[] = ['create_time', '<', $end_time];
        } else if ($start_time) {
            $map[] = ['create_time', '>', $start_time];
        }
    }


    public function attachedIndex($data)
    {
        foreach($data as $k=>$v)
        {
            //排料图纸
            $component = Component::where('drawing_no', '=', $v['drawing_no'])
                ->field('id,drawing_img,pid,number')
                ->find();

            $data[$k]['drawing_img'] = (string)$component['drawing_img'];
            $data[$k]['full_drawing_img'] = return_upload_url($component['drawing_img']);
            //项目名称
            $title = Projects::where('id', '=', $v['projects_id'])->value('title');
            $data[$k]['project_title'] = (string)$title;
            //订单
            $order_name = ProjectsOrder::where('id', $v['projects_order_id'])->value('name');
            $data[$k]['order_name'] = (string)$order_name;
            //父级
            if(!empty(empty($component['pid'])))
            {
                $data[$k]['p_name'] = '';
            }else{
                $p_mame = ProjectsOrder::where('id', '=', $component['pid'])->value('name');
                $data[$k]['p_name'] = (string)$p_mame;
            }
            //子级
            $son_num = Component::where('pid', '=', $component['id'])->count();
            $data[$k]['son_num'] = intval($son_num);
            //数量
            $data[$k]['number'] = intval($component['number']);
            //外购件信息
            $data[$k]['purchased_lists'] =(new CompReCompPurch)->componentInPurchased($component['id']);
            //交付时间
            $estimated_delivery_time = ProjectsOrder::where('id', '=', $v['projects_order_id'])->value('estimated_delivery_time');
            $data[$k]['estimated_delivery_time'] = intval($estimated_delivery_time);
            //负责人
            $data[$k]['responsible'] = (new UserS)->getUserName($v['responsible_id']);
            //todo  确认二维码链接
            //二维码
            $data[$k]['qrcode'] = '测试链接';

        }
        return $data;
    }


}